package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.secret.SecretAESDESede;
import com.zcx.helper.util.UtilMD5;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.mystatic.publicUrl;
import longcaisuyun.longcai.com.mystatic.publicstatic;
import longcaisuyun.longcai.com.net.PostAddCard;
import longcaisuyun.longcai.com.net.PostEditPassword;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//

/************************************************************/

public class MakeSureActivity extends Activity implements View.OnClickListener {

    TextView tv_1, tv_2, tv_3, tv_4, tv_5, tv_6;
    Button bt_next;
    LinearLayout l_mima;
    int num = 0;
    String s = "";
    //跳转到本页的模式，1为忘记密码。2位修改密码
    int j = 0;
    List<TextView> list = new ArrayList<TextView>();
    Bundle bundle;
    Intent i;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_sure);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        i = getIntent();
        bundle = i.getExtras();
        Log.e("我是password", bundle.getString("password"));
        ViewInit();
        publicstatic.list_mima.add(this);
    }

    private void ViewInit() {
        LinearLayout fanhui_lin = (LinearLayout) findViewById(R.id.fanhui_lin);
        fanhui_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt_next = (Button) findViewById(R.id.bt_done);
        bt_next.setOnClickListener(this);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        tv_5 = (TextView) findViewById(R.id.tv_5);
        tv_6 = (TextView) findViewById(R.id.tv_6);
        l_mima = (LinearLayout) findViewById(R.id.l_mima);
        l_mima.setOnClickListener(this);
        list.add(tv_1);
        list.add(tv_2);
        list.add(tv_3);
        list.add(tv_4);
        list.add(tv_5);
        list.add(tv_6);
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.l_mima:
                showpop();
                break;
            case R.id.b_1:
                add("1");
                break;
            case R.id.b_2:
                add("2");
                break;
            case R.id.b_3:
                add("3");
                break;
            case R.id.b_4:
                add("4");
                break;
            case R.id.b_5:
                add("5");
                break;
            case R.id.b_6:
                add("6");
                break;
            case R.id.b_7:
                add("7");
                break;
            case R.id.b_8:
                add("8");
                break;
            case R.id.b_9:
                add("9");
                break;
            case R.id.b_0:
                add("0");
                break;
            case R.id.b_del:
                del();
                break;
            case R.id.bt_done:
                if (tv_6.getText().toString().equals("")) {
                    Toast.makeText(MakeSureActivity.this, "请输入完整的支付密码再点击确认", Toast.LENGTH_SHORT).show();
                } else {
                    if (bundle.getString("password").equals(getpassword())) {
                        if (bundle.getString("model").equals("3")){
                            MyApplication.myviewutil.startProgressDialog(MakeSureActivity.this);
                            new PostAddCard(MyApplication.myPreferences.readUid(), bundle.getString("name"), bundle.getString("num"), bundle.getString("type"), bundle.getString("mobile"), getpassword(), new AsyCallBack<PostAddCard.Info>() {
                                @Override
                                public void onSuccess(String toast, int type,PostAddCard.Info  info ) throws Exception {
                                    super.onSuccess(toast, type, info);
                                    if(info.message.equals("0")){
                                        Toast.makeText(MakeSureActivity.this, "添加失败", Toast.LENGTH_SHORT).show();
                                    }else{
                                        Toast.makeText(MakeSureActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
                                        finishmima();
                                    }
                                    MyApplication.myviewutil.stopProgressDialog();
                                }

                                @Override
                                public void onFail(String toast, int type) throws Exception {
                                    super.onFail(toast, type);
                                    MyApplication.myviewutil.stopProgressDialog();
                                    Toast.makeText(MakeSureActivity.this, "添加失败,请检查您的网络后再进行尝试", Toast.LENGTH_SHORT).show();
                                }

                            }).execute(MakeSureActivity.this);
                        }else if (bundle.getString("model").equals("1")){
                            httpPost_ok();
                        }
                        else{
                            MyApplication.myviewutil.startProgressDialog(MakeSureActivity.this);
                            new PostEditPassword(MyApplication.myPreferences.readUid(), bundle.getString("pass") ,getpassword(),"2", new AsyCallBack<PostEditPassword.Info>() {
                                @Override
                                public void onSuccess(String toast, int type, PostEditPassword.Info info) throws Exception {
                                    super.onSuccess(toast, type, info);
                                    if (info.message.equals("0")){
                                        Toast.makeText(MakeSureActivity.this, "修改支付密码失败", Toast.LENGTH_SHORT).show();
                                    }else {
                                        Toast.makeText(MakeSureActivity.this, "修改支付密码成功", Toast.LENGTH_SHORT).show();
                                        finishmima();
                                    }
                                    MyApplication.myviewutil.stopProgressDialog();
                                }

                                @Override
                                public void onFail(String toast, int type) throws Exception {
                                    super.onFail(toast, type);
                                    MyApplication.myviewutil.stopProgressDialog();
                                    Toast.makeText(MakeSureActivity.this, "修改失败,请检查您的网络后再进行尝试", Toast.LENGTH_SHORT).show();
                                }
                            }).execute(MakeSureActivity.this);
                        }

                    } else {
                        Toast.makeText(MakeSureActivity.this, "新密码输入不一致，请检查后重新输入", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    private void httpPost_ok() {
        final SecretAESDESede secretAESDESede = new SecretAESDESede("123456781234567812345678", "12345678", SecretAESDESede.DESEDE_CBC_PKCS5PADDING);
        final FinalHttp finalHttp = new FinalHttp();
        AjaxParams params = new AjaxParams();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile", bundle.getString("tel"));
            jsonObject.put("newpwd", UtilMD5.MD5EncodeCount(getpassword(),"",2));
            jsonObject.put("action", "2");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            params.put("Keys", secretAESDESede.encrypt(jsonObject.toString()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        finalHttp.post(publicUrl.wangji, params, new AjaxCallBack<String>() {
            public void onStart() {
                super.onStart();
                MyApplication.myviewutil.startProgressDialog(MakeSureActivity.this, finalHttp, publicUrl.mobilecode);
            }

            public void onSuccess(String s) {
                super.onSuccess(s);
                try {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(secretAESDESede.decrypt(s));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    String message = jsonObject.getString("message");
                    if (message.equals("0")) {
                        Toast.makeText(MakeSureActivity.this, "重置支付密码失败", Toast.LENGTH_SHORT).show();
                    }
                    if (message.equals("1")) {
                        Toast.makeText(MakeSureActivity.this, "重置支付密码成功", Toast.LENGTH_SHORT).show();
                        finishmima();
                    }
                    if (message.equals("2")) {
                        Toast.makeText(MakeSureActivity.this, "手机号码不存在，请检查后重新尝试", Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

                MyApplication.myviewutil.stopProgressDialog();
            }

            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                MyApplication.myviewutil.stopProgressDialog();
            }
        });
    }
    public void finishmima() {
        for (int j = 0; j < publicstatic.list_mima.size(); j++) {
            publicstatic.list_mima.get(j).finish();
        }
        publicstatic.list_mima.clear();
    }

    public String getpassword() {
        String s = "";
        for (int j = 0; j < list.size(); j++) {
            s += list.get(j).getText().toString();
        }
        return s;
    }

    public void add(String j) {

        if (num < 6) {
            list.get(num).setText(j);
            if (num < 6)
                num++;
        }
        if (num==6){

        }
    }

    public void del() {
        Log.e("num", num + "");
        if (num == 6) {
            num = 5;
            list.get(num).setText("");
        } else {
            list.get(num).setText("");
        }

        if (num > 0) num--;
    }

    public void showpop() {
        View view = View.inflate(MakeSureActivity.this, R.layout.item_input, null);

        final PopupWindow window = new PopupWindow(MyApplication.ScaleScreenHelper.loadView((ViewGroup) view),
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        window.setAnimationStyle(R.style.mypopwindow_anim_style);

        // 设置popWindow弹出窗体可点击
        window.setFocusable(true);
        window.setOutsideTouchable(false);
        window.setOutsideTouchable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable();
        window.setBackgroundDrawable(dw);
        window.showAtLocation(l_mima, Gravity.BOTTOM, 0, 0);
        Button b_1 = (Button) view.findViewById(R.id.b_1);
        Button b_2 = (Button) view.findViewById(R.id.b_2);
        Button b_3 = (Button) view.findViewById(R.id.b_3);
        Button b_4 = (Button) view.findViewById(R.id.b_4);
        Button b_5 = (Button) view.findViewById(R.id.b_5);
        Button b_6 = (Button) view.findViewById(R.id.b_6);
        Button b_7 = (Button) view.findViewById(R.id.b_7);
        Button b_8 = (Button) view.findViewById(R.id.b_8);
        Button b_9 = (Button) view.findViewById(R.id.b_9);
        Button b_0 = (Button) view.findViewById(R.id.b_0);
        Button b_del = (Button) view.findViewById(R.id.b_del);
        b_1.setOnClickListener(this);
        b_2.setOnClickListener(this);
        b_3.setOnClickListener(this);
        b_4.setOnClickListener(this);
        b_5.setOnClickListener(this);
        b_6.setOnClickListener(this);
        b_7.setOnClickListener(this);
        b_8.setOnClickListener(this);
        b_9.setOnClickListener(this);
        b_0.setOnClickListener(this);
        b_del.setOnClickListener(this);
    }
}
